<?php
/*
CMSimple - Calendar Plugin - version 0.5 Final
 2006 - Michael Svarrer
---------------------------------------------------------------------
- Version 0.5 : 17. mar 2006
New feature added external linkage and miltiple dates for same event syntax
date delimiter can be /.- for input. For output the event table parameter
$plugin_cf['calendar']['date_delimiter'] controls the output
New CSS clases added for the event_monthyear, event_heading,event_data
Improved way of generating event table implemented
- Version 0.4 : 11. sept 2005
 Title added to link in calendar mode showing event
 $end_month added in the events function, here you specify the amount 
 of months the events function should show.
 Prev/Next month button avaliable, set 'prev_next_button' to true to enable
 Bug when using week_starts_monday = false
- Version 0.3 : Third release - 16. jun 2005
Parameters added to config
 $plugin_cf['calendar']['calendar_width']
 $plugin_cf['calendar']['event_width_date']="20%";
 $plugin_cf['calendar']['event_width_event']="60%";
 $plugin_cf['calendar']['event_width_location']="20%";
- Version 0.2 : Second release - 15. jun 2005
Some Notice: Undefined variable: for different variables
*/
function calendar($year,$month,$eventpage)
{
global $plugin_cf,$plugin_tx,$pth,$sl,$plugin,$sn,$su;
$plugin=basename(dirname(__FILE__),"/");

$eventfile = $pth['folder']['plugins'].$plugin."/content/eventcalendar_".$sl.".txt";
if(!is_file($eventfile)){$handle = fopen($eventfile, "w");
fclose($handle);}

$startmon = strtolower($plugin_cf[$plugin]['week_starts_mon']);

if($month==''){$month = isset($_GET['month']) ? $_GET['month'] : date("m",time());}
if($year==''){$year = isset($_GET['year']) ? $_GET['year'] : date("Y",time());}

$event_year_array = array();
$event_month_array = array(); 
$event_date_array = array(); 
$event_array = array(); 
$event_location_array = array(); 

$t='';$event_dates='';$eventdates='';$event_date='';$event_date_start='';$event_date_end='';$event_year='';$event_month='';$event='';$event_day='';$location='';$event_today='';$event_title='';

if(is_file($eventfile)){
$fp = fopen($eventfile, "r");
while (!feof($fp)){
   $line = fgets($fp, 4096);
 if(eregi(';', $line)){list($eventdates,$event,$location) = split( ";", $line);
list($event_date,$event_month,$event_year) = split( "[/.-]", $eventdates);
if(eregi(',', $event_date)){list($event_date_start,$event_date_end) = split(",",$event_date);}else{$event_date_start=$event_date;$event_date_end='';}}

if($event_date_end<>''){
for($i=$event_date_start; $i <= $event_date_end; $i++): 
 array_push($event_year_array, $event_year); 
 array_push($event_month_array, $event_month); 
 array_push($event_date_array, $i); 
 array_push($event_array, $event);
 array_push($event_location_array, $location); 
endfor;
}else{
 array_push($event_year_array, $event_year);
 array_push($event_month_array, $event_month); 
 array_push($event_date_array, $event_date_start);
 array_push($event_array, $event);
 array_push($event_location_array, $location); 
 }}
fclose($fp);
}
$month = (isset($month)) ? $month : date("n",time());
$textmonth = date("F",mktime(1,1,1,$month,1,$year)); 
  
$monthnames = explode(",", $plugin_tx['calendar']['monthnames_array']);

$textmonth = $monthnames[$month - 1]; 
	
  $year = (isset($year)) ? $year : date("Y",time()); 
  $today = (isset($today))? $today : date("j", time());  // Make $today really big to avoid hilighting 
  $today = ($month == date("n",time()) && $year == date("Y",time())) ? $today : 32;  //will only highlight today for this month & year 

  $days = date("t",mktime(1,1,1,$month,1,$year)); 

  $dayone = date("w",mktime(1,1,1,$month,1,$year)); 
  $daylast = date("w",mktime(1,1,1,$month,$days,$year)); 
  $dayarray = explode(",", $plugin_tx['calendar']['daynames_array']);


$t .="<table cellpadding='1' cellspacing='0' width='".$plugin_cf['calendar']['calendar_width']."' class='calendar_main'><tr>"; 
$t .="<td colspan='7'>";
$t .="<div class='calendar_monthyear'>$textmonth $year</div>";

if(eregi("true",$plugin_cf['calendar']['prev_next_button']))
{
if($month<=1){$month_prev=12;$year_prev=$year-1;}else {$month_prev=$month-1;$year_prev=$year;}
if($month>=12){$month_next=1;$year_next=$year+1;}else {$month_next=$month+1;$year_next=$year;}
$t .="<div class='calendar_monthyear'><a href=\"$sn?$su&amp;month=$month_prev&amp;year=$year_prev\" title=\"prev\"><<</a> <a href=\"$sn?$su&amp;month=$month_next&amp;year=$year_next\" title=\"next\">>></a></div>";
}

$t .="</td>";
$t .="</tr>";

    for($i=0; $i <= 6; $i++): 
     if($startmon=='true'){$j=$i+1;}else{$j=$i;}
     if($j==7)$j=0;
      $width = ($i == 0 || $i == 6) ? "15%" : "14%"; 

$t .="<td valign=\"middle\" width=\"$width\" class=\"calendar_daynames\">$dayarray[$j]</td>\n"; 
    endfor; 
$t .="</tr>\n";  //done printing the top row of days 

     if($startmon=='true'){$span1 = $dayone-1;}else{$span1 = $dayone;}
     if($span1==-1)$span1=6;

     if($startmon=='true'){$span2 = 7 - $daylast;}else{$span2 = 6 - $daylast;}

  for($i = 1; $i <= $days; $i++): 
    $dayofweek = date("w",mktime(1,1,1,$month,$i,$year));

     if($startmon=='true')$dayofweek=$dayofweek-1;
     if($dayofweek==-1)$dayofweek=6;

foreach($event_year_array as $keys=>$temp){
if($event_year_array[$keys] == $year 
   and $event_month_array[$keys] == $month 
   and $event_date_array[$keys]==$i){
   $event_day=$i;
   $external_site ='';
if($event_title){$event_title.="\r\n".strip_tags($event_array[$keys]);}else{$event_title=strip_tags($event_array[$keys]);}
}

if(trim($event_location_array[$keys])=='###' 
   and $event_month_array[$keys] == $month 
   and $event_date_array[$keys]==$i){
   $event_day=$i; 
   $age = $year-$event_year_array[$keys];
   $external_site ='';

if($event_title){$event_title.="\r\n".$event_array[$keys]." ".$age;}else{

   $event_title= $event_array[$keys]." ".$age;}

}

 }
    $width = ($dayofweek == 0 || $dayofweek == 6) ? "15%" : "14%"; //sets the col width 

    $tableday = $i;  

        if($i == 1 || $dayofweek == 0): 
      $t .= "<tr>\n"; 
      if($span1 > 0 && $i == 1) 
        $t .= "<td class='calendar_noday' colspan=\"$span1\"><font face=\"null\" size=\"1\">&nbsp;</font></td>\n"; 
    endif; 

if($today==$event_day)$event_today=$today;

    switch ($i): 
        case $event_today:
if($external_site){$t .="<td class=\"calendar_today\"><a href=\"http://".$external_site."\" target=\"_blank\" title=\"$event_title\">$tableday</a></td>\n";
}else{$t .="<td class=\"calendar_today\"><a href=\"?".$eventpage."&amp;month=$month&amp;year=$year\" title=\"$event_title\">$tableday</a></td>\n";$event_title='';}
              break; 
        case $today: 
        $t .="<td class='calendar_today'>$tableday</td>\n"; 
              break; 
        case $event_day: 
if($external_site){$t .="<td class=\"calendar_eventday\"><a href=\"http://".$external_site."\" target=\"_blank\" title=\"$event_title\">$tableday</a></td>\n";}
else{$t .="<td class=\"calendar_eventday\"><a href=\"?".$eventpage."&amp;month=$month&amp;year=$year\" title=\"$event_title\">$tableday</a></td>\n";$event_title='';} 
              break; 
   
      default:
        $t .="<td class='calendar_day'>$tableday</td>\n"; 
    endswitch; 

    if($i == $days && $span2 > 0) 
      $t .= "<td class='calendar_noday' colspan=\"$span2\"><font face=\"null\" size=\"1\">&nbsp;</font></td>\n"; 
    if($dayofweek == 6 || $i == $days) 
      $t .= " </tr>\n"; 
  endfor; 
  $t .= "</table>\n"; 

return $t;
}
// **************************************************
// * Events plugin                                  *
// **************************************************

function events($month,$year,$end_month)
{
global $plugin_cf,$plugin_tx,$pth,$sl,$plugin;

$month_input = isset($_GET['month']) ? $_GET['month'] : ''; 
$month_input .= isset($_POST['month']) ? $_POST['month'] : ''; 

if($month){ 
if($month_input){ 
if($month>=$month_input)$month=$month_input;} 
}else{$month=$month_input;} 

$year = isset($_GET['year']) ? $_GET['year'] : '';
$year .= isset($_POST['year']) ? $_POST['year'] : '';

if($month==''){$month = date("m",time());}
if($year==''){$year = date("Y",time());}
if($end_month==''){$end_month= "1";}

$plugin=basename(dirname(__FILE__),"/");

$event_year_array = array(); 
$event_month_array = array(); 
$event_date_array = array(); 
$event_array = array(); 
$event_location_array = array(); 

$eventfile = $pth['folder']['plugins'].$plugin."/content/eventcalendar_".$sl.".txt";

if(is_file($eventfile)){
$fp = fopen($eventfile, "r");
while (!feof($fp)) {
   $line = fgets($fp, 4096);
 list($eventdates,$event,$location) = split( ";", $line);
 list($event_date,$event_month,$event_year) = split( "[/.-]", $eventdates);
 list($event_date_start,$event_date_end) = split(",",$event_date);
if($event_date_end){
for($i=$event_date_start; $i <= $event_date_end; $i++): 
 array_push($event_year_array, $event_year); 
 array_push($event_month_array, $event_month); 
 array_push($event_date_array, $i); 
 array_push($event_array, $event);
 array_push($event_location_array, $location); 
endfor;
}else{
 array_push($event_year_array, $event_year); 
 array_push($event_month_array, $event_month); 
 array_push($event_date_array, $event_date); 
 array_push($event_array, $event);
 array_push($event_location_array, $location); 
 } }
fclose($fp);
}

$x=1;

$t .="<table border='0' width='100%'>";

while($x<=$end_month){
$textmonth = date("F",mktime(1,1,1,$month,1,$year)); 
$monthnames = explode(",", $plugin_tx['calendar']['monthnames_array']);

  $textmonth = $monthnames[$month - 1]; 
  $today = (isset($today))? $today : date("j", time());  // Make $today really big to avoid hilighting 
  $today = ($month == date("m",time()) && $year == date("Y",time())) ? $today : 32;  //will only highlight today for this month & year 

$table=false;

if (in_array($year,$event_year_array)){
if (in_array($month,$event_month_array)){
$table=true;
}}
if($table){
$t .="<tr>";
$t .="<td class='event_monthyear' colspan=3><br><b>$textmonth $year</b><br></td>";
$t .="</tr>";
$t .="<tr>";
$t .="<td class='event_heading' width='".$plugin_cf['calendar']['event_width_date']."'>".$plugin_tx['calendar']['event_date']."</td>";
$t .="<td class='event_heading' width='".$plugin_cf['calendar']['event_width_event']."'>".$plugin_tx['calendar']['event_event']."</td>";
$t .="<td class='event_heading' width='".$plugin_cf['calendar']['event_width_location']."'>".$plugin_tx['calendar']['event_location']."</td>";
$t .="</tr>";
}
asort($event_date_array);
foreach($event_date_array as $keys=>$temp){
if(trim($event_location_array[$keys])=='###' and $event_month_array[$keys] == $month )
{
$age = $year-$event_year_array[$keys];
if ($age >= 0){
if ($month<10){if (strlen($month)==1){$month='0'.$month;}}
if (!$table){
$table=true;
$t .="<tr>";
$t .="<td class='calendar_monthyear' colspan=3><br><b>$textmonth $year</b><br></td>";
$t .="</tr>";
$t .="<tr>";
$t .="<td class='event_data' width='".$plugin_cf['calendar']['event_width_date']."'>".$plugin_tx['calendar']['event_date']."</td>";
$t .="<td class='event_data' width='".$plugin_cf['calendar']['event_width_event']."'>".$plugin_tx['calendar']['event_event']."</td>";
$t .="<td class='event_data' width='".$plugin_cf['calendar']['event_width_location']."'>".$plugin_tx['calendar']['event_location']."</td>";
$t .="</tr>";

}
$t .="<tr>";
$t .="<td class='event_data' width='".$plugin_cf['calendar']['event_width_date']."'>$event_date_array[$keys]".$plugin_cf['calendar']['date_delimiter']."$month".$plugin_cf['calendar']['date_delimiter']."$year</td>";
$t .="<td class='event_data' width='".$plugin_cf['calendar']['event_width_event']."'>".$event_array[$keys]." $age ".$plugin_tx['calendar']['age_text']."</td>";
$t .="<td class='event_data' width='".$plugin_cf['calendar']['event_width_location']."'>".$plugin_tx['calendar']['birthday_text']."</td>";
$t .="</tr>";}
}

if($event_year_array[$keys] == $year and $event_month_array[$keys] == $month)
{
if ($month<10){if (strlen($month)==1){$month='0'.$month;}}
$t .="<tr>";
$t .="<td class='event_data' width='".$plugin_cf['calendar']['event_width_date']."'>$event_date_array[$keys]".$plugin_cf['calendar']['date_delimiter']."$month".$plugin_cf['calendar']['date_delimiter']."$year</td>";
$t .="<td class='event_data' width='".$plugin_cf['calendar']['event_width_date']."'>".$event_array[$keys]."</td>";

if(eregi('ext:', $event_location_array[$keys])){
$external_site = substr($event_location_array[$keys],4);
list($external_site,$external_text) = split(",",$external_site); 
if(!$external_text)$external_text=$external_site;
$t .="<td class='event_data' width='".$plugin_cf['calendar']['event_width_date']."'><a href=\"http://".$external_site."\" target=\"_blank\" title=\"".strip_tags($event_array[$keys])."\">$external_text</a></td>";
}
elseif(eregi('int:', $event_location_array[$keys])){
$internal_page = substr($event_location_array[$keys],4);
list($internal_page,$internal_text) = split(",",$internal_page);
if(!$internal_text)$internal_text=$internal_page;
$t .="<td class='event_data' width='".$plugin_cf['calendar']['event_width_date']."'><a href=\"?".$internal_page."\" title=\"".strip_tags($event_array[$keys])."\">$internal_text</a></td>";
}
else{$t .="<td class='event_data' width='".$plugin_cf['calendar']['event_width_date']."'>".strip_tags($event_location_array[$keys])."</td>";}
$t .="</tr>";
}
}
$x++;
if($month==12){$year++;$month=1;}else{$month++;}
}
$t .="</table>";
return $t;}
?>
